package com.demo.controller;

import com.demo.entity.Product;
import com.demo.entity.ProductRecord;
import com.demo.entity.User;
import com.demo.repository.ProductRecordRepo;
import com.demo.repository.ProductRepo;
import com.demo.service.impl.RecordServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by john on 2017/6/21.
 */
@RestController
public class RecordFormController {

    @Autowired
    private RecordServiceImpl recordServiceImpl;

    @Autowired
    private ProductRepo productRepo;






    @RequestMapping(value = "/recordform")
    public ProductRecord insertrecordform(@RequestBody  ProductRecord productRecord, HttpSession session, HttpServletResponse response) throws IOException {
        User user = (User) session.getAttribute("user");
        int userid = user.getId();
        System.out.print(userid);
        ProductRecord productRecordGet =recordServiceImpl.save(productRecord.getCustomerName(),productRecord.getCustomerPhone(),productRecord.getProductId(),userid);


        return productRecordGet;
    }

    @RequestMapping(value = "/getproduct")
    public List<Product> queryProduct(){


         return (List<Product>)productRepo.findAll();


    }

}
